package com.ruoyi.safe.controller.web;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.safe.service.IBizSafeBillnoService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.safe.domain.BizSafeFine;
import com.ruoyi.safe.service.IBizSafeFineService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 安全罚款台账Controller
 *
 * @author ruoyi
 * @date 2023-11-11
 */
@RestController
@RequestMapping("/fine/fine")
public class BizSafeFineController extends BaseController
{
    @Autowired
    private IBizSafeFineService bizSafeFineService;
    @Autowired
    private IBizSafeBillnoService bizSafeBillnoService;

    /**
     * 查询安全罚款台账(对分包罚款)列表
     */
    @PreAuthorize("@ss.hasPermi('fine:fine:list')")
    @GetMapping("/subPackageList")
    public TableDataInfo subPackageList(BizSafeFine bizSafeFine)
    {
        startPage();
        PageUtils.orderBy("create_time desc");
        bizSafeFine.setFineButton("1");
        List<BizSafeFine> list = bizSafeFineService.selectBizSafeFineList(bizSafeFine);
        return getDataTable(list);
    }

    /**
     * 查询安全罚款台账(对项目罚款)列表
     * @param bizSafeFine
     * @return
     */
    @PreAuthorize("@ss.hasPermi('fine:fine:list')")
    @GetMapping("/programList")
    public TableDataInfo programList(BizSafeFine bizSafeFine)
    {
        startPage();
        PageUtils.orderBy("create_time desc");
        bizSafeFine.setFineButton("2");
        List<BizSafeFine> list = bizSafeFineService.selectProgramList(bizSafeFine);
        return getDataTable(list);
    }

    /**
     * 导出安全罚款台账列表
     */
    @PreAuthorize("@ss.hasPermi('fine:fine:export')")
    @Log(title = "质量罚款或扣分台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizSafeFine bizSafeFine)
    {
        boolean bButton1 = bizSafeFine.getFineButton().equals("1");
        List<BizSafeFine> list = bizSafeFineService.selectBizSafeFineList(bizSafeFine);
        ExcelUtil<BizSafeFine> util = new ExcelUtil<BizSafeFine>(BizSafeFine.class);
        util.exportExcel(response, list, bButton1 ? "质量罚款台账数据" : "质量扣分台账数据");
    }

    /**
     * 获取安全罚款台账详细信息
     */
    @PreAuthorize("@ss.hasPermi('fine:fine:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizSafeFineService.selectBizSafeFineById(id));
    }

    /**
     * 新增安全罚款台账(对分包罚款)
     */
    @PreAuthorize("@ss.hasPermi('fine:fine:add')")
    @Log(title = "安全罚款台账", businessType = BusinessType.INSERT)
    @PostMapping("/addSubPackage")
    public AjaxResult addSubPackage(@RequestBody BizSafeFine bizSafeFine)
    {
        return toAjax(bizSafeFineService.insertBizSafeFine(bizSafeFine, SecurityUtils.getLoginUser().getUser()));
    }

    /**
     * 新增安全罚款台账(对项目罚款)
     */
    @PreAuthorize("@ss.hasPermi('fine:fine:add')")
    @Log(title = "安全罚款台账", businessType = BusinessType.INSERT)
    @PostMapping("/addProgram")
    public AjaxResult addProgram(@RequestBody BizSafeFine bizSafeFine)
    {
        return toAjax(bizSafeFineService.insertProgram(bizSafeFine, SecurityUtils.getLoginUser().getUser()));
    }

    /**
     * 修改安全罚款台账
     */
    @PreAuthorize("@ss.hasPermi('fine:fine:edit')")
    @Log(title = "安全罚款台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizSafeFine bizSafeFine)
    {
        return toAjax(bizSafeFineService.updateBizSafeFine(bizSafeFine, SecurityUtils.getLoginUser().getUser()));
    }

    /**
     * 删除安全罚款台账
     */
    @PreAuthorize("@ss.hasPermi('fine:fine:remove')")
    @Log(title = "安全罚款台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizSafeFineService.deleteBizSafeFineByIds(ids));
    }

    /**
     * 点击新增时获取新编号
     * @return
     */
    @GetMapping("/getCode")
    public AjaxResult getCode(Long id){
        AjaxResult ajaxResult = new AjaxResult();
        String safeNumber = bizSafeBillnoService.getSafeNumber(Long.valueOf(id));
        ajaxResult.put("safeNumber",safeNumber);
        return ajaxResult;
    }
}
